package com.cf.game.cowboy;

import com.cf.game.Player;
import com.cf.game.cowboy.handler.HandlerContext;
import com.cf.game.service.constants.EnumH5CowBoyGameState;
import com.cf.game.service.pojo.H5Game;
import com.cf.game.service.pojo.H5GameInstance;
import com.cf.game.service.pojo.UserInfo;
import com.cf.game.service.util.BeanFactoryUtil;
import com.cf.serialize.Message;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.Session;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * cf-game-Game
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/3/27 10:52
 */
@Slf4j
public class CowBoyGame {

    public H5Game game;

    public volatile H5GameInstance h5GameInstance;

    public static final Long Stake_Time = 30 * 1000L;

    public volatile EnumH5CowBoyGameState state = EnumH5CowBoyGameState.STAKE;

    public Map<Integer, Player> players = new ConcurrentHashMap();

    private Map<Session, Integer> playerSession = new ConcurrentHashMap();

    public GameStateDetail gameStateDetail = new GameStateDetail();

    public void init(H5Game h5Game) {
        this.game = h5Game;
        //初始化
        CowBoyTimingWheel.timingWheel.add(new StateExpire(this, EnumH5CowBoyGameState.LOTTERY, EnumH5CowBoyGameState.STAKE), 10 * 1000L);
    }


    public void handleMessage(Player player, Message message) {
        MessageHandler suitableMessageHandler = BeanFactoryUtil.getBean(HandlerContext.class).getSuitableMessageHandler(message);
        if (suitableMessageHandler != null) {
            suitableMessageHandler.handle(player, this, message);
        }
    }


    public void refreshAndStart(H5GameInstance h5GameInstance) {
        this.h5GameInstance = h5GameInstance;
        gameStateDetail.refresh();
    }


    public void broadcast(Message message) {
        Collection<Player> players = this.players.values();
        for (Player player : players) {
            unicast(player, message);
        }
    }


    public void unicast(Player player, Message message) {
        Session session = player.getSession();
        if (session.isOpen()) {
            player.unicast(message);
        }
    }


    public void enterGame(Player player) {
        if (this.players.get(player.getUserInfo().getId()) != null) {
            return;
        }
        this.players.put(player.getUserInfo().getId(), player);
        this.playerSession.put(player.getSession(), player.getUserInfo().getId());
    }


    public void leaveGame(Session playerSession) {
        Integer playId = this.playerSession.remove(playerSession);
        if (playId != null) {
            Player player = this.players.remove(playId);
            if (player != null) {
                if (player.getSession() != null && player.getSession().isOpen()) {
                    try {
                        player.getSession().close();
                    } catch (IOException e) {
                        log.error("关闭连接异常！");
                    }
                }
            }
        }
    }


    public void updateState(EnumH5CowBoyGameState enumH5CowBoyGameState) {
        this.state = enumH5CowBoyGameState;
    }


    public String getGameCode() {
        return "cowboy";
    }


    public String getGameId() {
        return game.getId();
    }


    public Player enterGame(UserInfo userInfo, Session session) {
        Player player = this.players.get(userInfo.getId());
        if (player != null) {
            return player;
        } else {
            player = new Player(userInfo, session);
        }
        this.players.put(player.getUserInfo().getId(), player);
        this.playerSession.put(player.getSession(), player.getUserInfo().getId());
        return player;
    }
}
